<template>
	<view class="container">
		<view class="page-body">
			<view class='wrapper'>
				<view class='toolbar' @tap="format" >
					<view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-bold" data-name="bold" data-value="bold">
					</view>
					<view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-italic" data-name="italic" data-value="italic">
					</view>
					<view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-underline"
						data-name="underline"></view>
					<view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-strikethrough"
						data-name="strike"></view>
					<!-- #ifndef MP-BAIDU -->
					<view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-align-left"
						data-name="align" data-value="left"></view>
					<!-- #endif -->
					<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-align-center"
						data-name="align" data-value="center"></view>
					<view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-align-right"
						data-name="align" data-value="right"></view>
					<view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-justify"
						data-name="align" data-value="justify"></view>
					<!-- #ifndef MP-BAIDU -->
					<view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height"
						data-name="lineHeight" data-value="2"></view>
					<view :class="formats.letterSpacing ? 'ql-active' : ''" class="iconfont icon-letterSpacing"
						data-name="letterSpacing" data-value="2em"></view>
					<view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-paragraphMarginTop"
						data-name="marginTop" data-value="20px"></view>
					<view :class="formats.marginBottom ? 'ql-active' : ''" class="iconfont icon-24gl-paragraphMarginBottom"
						data-name="marginBottom" data-value="20px"></view>
					<!-- #endif -->
<!-- 
					<view class="iconfont icon-clearedformat" @tap="removeFormat"></view> -->

					<!-- #ifndef MP-BAIDU -->
					<view :class="formats.fontFamily ? 'ql-active' : ''" class="iconfont icon-font-family"
						data-name="fontFamily" data-value="Pacifico"></view>
<!-- 					<view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-fontsize"
						data-name="fontSize" data-value="24px"></view> -->
					<!-- #endif -->
					<view :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-font-size"
						data-name="color" data-value="#0000ff"></view>
					<view :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''"
						class="iconfont icon-editor-background-color" data-name="backgroundColor" data-value="#00ff00"></view>
					<view class="iconfont icon-icon_date" @tap="insertDate"></view>
					<view class="iconfont icon-icon_checklist" data-name="list" data-value="check"></view>
					<view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-list"
						data-name="list" data-value="ordered"></view>
					<!-- <view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie"
						data-name="list" data-value="bullet"></view> -->

					<view class="iconfont icon-icon_undo" @tap="undo"></view>
					<view class="iconfont icon-icon_redo" @tap="redo"></view>

					<view class="iconfont icon-iconoutdent" data-name="indent" data-value="-1"></view>
					<view class="iconfont icon-icon_indent" data-name="indent" data-value="+1"></view>
					<view class="iconfont icon-fengexian" @tap="insertDivider"></view>
					<view class="iconfont icon-charutupian" @tap="insertImage"></view>
					<view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-formatheader1"
						data-name="header" :data-value="1"></view>
<!-- 					<view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao"
						data-name="script" data-value="sub"></view>
					<view :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao"
						data-name="script" data-value="super"></view> -->

					<view class="iconfont icon-shanchu" @tap="clear"></view>

<!-- 					<view :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl"
						data-name="direction" data-value="rtl"></view> -->
				</view>

				<view class="editor-wrapper">
					<editor id="editor" class="ql-container" placeholder="开始输入..." show-img-size show-img-toolbar
						show-img-resize @statuschange="onStatusChange" :read-only="readOnly" 
						ref="editorCtx"
						>
					</editor>
				</view>
			</view>
		</view>
	</view>
	
</template>


<script setup >
import { reactive, ref, onMounted, getCurrentInstance} from 'vue'
import { upload,strFn} from "../../lib/ts/util"
	
	
	//获取内容
	const getContents = ()=>{
		return new Promise((resolve,reject)=>{
			editorCtx.getContents().then(res=>{
				resolve(res.html)
			})
		})
	}
	

	
	let readOnly = ref(false)
	let formats = reactive({})
	let editorCtx = null
	onMounted(()=>{
		onEditorReady()
	})
	
	// // #ifndef MP-BAIDU
	// uni.loadFontFace({
	// 	family: 'Pacifico',
	// 	source: 'url("https://sungd.github.io/Pacifico.ttf")'
	// })
	// // #endif

	
	const readOnlyChange = ()=>{ readOnly.value = !readOnly.value }
	const onEditorReady = (e)=>{
		// #ifdef MP-BAIDU
		editorCtx = requireDynamicLib('editorLib').createEditorContext('editor');
		// #endif

		// #ifdef APP-PLUS || MP-WEIXIN || H5
		const query = uni.createSelectorQuery().in(getCurrentInstance())
		query.select('#editor').context((res) => {
			editorCtx = res.context
		}).exec()
		// #endif
	}
	
	
	
	const undo = ()=> {editorCtx.undo()}
	const redo = ()=> {editorCtx.redo()}
	const format = (e)=>{
		let {
			name,
			value
		} = e.target.dataset
		if (!name) return
		if(formats[name]){
			 delete formats[name]
			 editorCtx.removeFormat()
		}else{
			editorCtx.format(name, value)
		}
		
	}
	const onStatusChange =(e)=> {
		const formats2 = e.detail
		for(let k in formats2){
			formats[k] = formats2[k]
		}
	}
	const insertDivider = ()=> {
		editorCtx.insertDivider({
			success: function() {
				
			}
		})
	}
	const clear = ()=> {
		uni.showModal({
			title: '清空编辑器',
			content: '确定清空编辑器全部内容？',
			success: res => {
				if (res.confirm) {
					editorCtx.clear({
						success: function(res) {
						}
					})
				}
			}
		})
	}
	const removeFormat = ()=> {
		editorCtx.removeFormat()
	}
	const insertDate = ()=>{
		const date = new Date()
		const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
		editorCtx.insertText({
			text: formatDate
		})
	}
	const insertImage = ()=>{
		uni.chooseImage({
			count: 1,
			success: (res) => {
				upload(res.tempFilePaths[0]).then(res=>{
					editorCtx.insertImage({
						src: strFn.img(res.data.url),
						alt: '图像',
						success: function() {
						}
					})
				})
			}
		})
	}
	
	defineExpose({
		getContents
	})
	
</script>


<style scoped lang="scss">
	@import url("./font_4589399_zt110o2k6r/iconfont.css");

	.editor-wrapper {
		background: #fff;
	}

	.iconfont {
		display: inline-block;
		padding: 12rpx;
		// width: 24px;
		// height: 24px;
		cursor: pointer;
		font-size: 20px;
	}

	.toolbar {
		box-sizing: border-box;
		border-bottom: 0;
		font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
	}

	.ql-container {
		box-sizing: border-box;
		padding: 12rpx 15rpx;
		width: 100%;
		height: 40vh;
		margin-top: 20rpx;
		font-size: 28rpx;
		line-height: 1.5;
	}

	.ql-active {
		color: $main-color;
	}
</style>
